{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "%reload_ext autoreload\n",
    "%autoreload 2\n",
    "%matplotlib inline\n",
    "import os\n",
    "os.environ[\"CUDA_DEVICE_ORDER\"]=\"PCI_BUS_ID\";\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"]=\"0\" \n",
    "import sys\n",
    "import ktrain\n",
    "from ktrain import vision as vis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Download the **Dogs vs. Cats** dataset from [here](https://www.kaggle.com/c/dogs-vs-cats) and set DATADIR to the extracted folder."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 23000 images belonging to 2 classes.\n",
      "Found 23000 images belonging to 2 classes.\n",
      "Found 2000 images belonging to 2 classes.\n",
      "The normalization scheme has been changed for use with a pretrained_resnet50 model. If you decide to use a different model, please reload your dataset with a ktrain.vision.data.images_from* function.\n",
      "\n",
      "Is Multi-Label? False\n",
      "pretrained_resnet50 model created.\n"
     ]
    }
   ],
   "source": [
    "DATADIR = 'data/dogscats'\n",
    "(train_data, val_data, preproc) = vis.images_from_folder(\n",
    "                                              datadir=DATADIR,\n",
    "                                              data_aug = vis.get_data_aug(horizontal_flip=True),\n",
    "                                              train_test_names=['train', 'valid'], \n",
    "                                              target_size=(224,224), color_mode='rgb')\n",
    "model = vis.image_classifier('pretrained_resnet50', train_data, val_data, freeze_layers=15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "learner = ktrain.get_learner(model=model, train_data=train_data, val_data=val_data, \n",
    "                             workers=8, use_multiprocessing=False, batch_size=64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "begin training using onecycle policy with max lr of 0.0001...\n",
      "Epoch 1/3\n",
      "  2/359 [..............................] - ETA: 29:33 - loss: 1.9746 - acc: 0.5000"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.6/dist-packages/keras/callbacks.py:122: UserWarning: Method on_batch_end() is slow compared to the batch update (0.436140). Check your callbacks.\n",
      "  % delta_t_median)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "359/359 [==============================] - 133s 371ms/step - loss: 0.3377 - acc: 0.9052 - val_loss: 0.0678 - val_acc: 0.9820\n",
      "Epoch 2/3\n",
      "359/359 [==============================] - 124s 345ms/step - loss: 0.1049 - acc: 0.9723 - val_loss: 0.0361 - val_acc: 0.9865\n",
      "Epoch 3/3\n",
      "359/359 [==============================] - 124s 346ms/step - loss: 0.0578 - acc: 0.9819 - val_loss: 0.0253 - val_acc: 0.9920\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x7f77fc6c5400>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "learner.fit_onecycle(1e-4, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "begin training using onecycle policy with max lr of 0.0001...\n",
      "Epoch 1/3\n",
      "359/359 [==============================] - 124s 347ms/step - loss: 0.0449 - acc: 0.9866 - val_loss: 0.0256 - val_acc: 0.9925\n",
      "Epoch 2/3\n",
      "359/359 [==============================] - 125s 348ms/step - loss: 0.0551 - acc: 0.9816 - val_loss: 0.0260 - val_acc: 0.9890\n",
      "Epoch 3/3\n",
      "359/359 [==============================] - 124s 345ms/step - loss: 0.0365 - acc: 0.9881 - val_loss: 0.0179 - val_acc: 0.9935\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x7f77f6ea7da0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "learner.fit_onecycle(1e-4, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "begin training using onecycle policy with max lr of 0.0001...\n",
      "Epoch 1/1\n",
      "359/359 [==============================] - 125s 347ms/step - loss: 0.0353 - acc: 0.9879 - val_loss: 0.0275 - val_acc: 0.9925\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x7f77f6e24240>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "learner.fit_onecycle(1e-4, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "begin training using onecycle policy with max lr of 2e-05...\n",
      "Epoch 1/1\n",
      "359/359 [==============================] - 124s 345ms/step - loss: 0.0223 - acc: 0.9916 - val_loss: 0.0170 - val_acc: 0.9955\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x7f77fce04940>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "learner.fit_onecycle(1e-4/5, 1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
